package org.alino.core.shiro.token;

import org.alino.core.utils.LoggerUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;

/**
 * @Desc TODO
 * @Author by Brant
 * @Date 2017/1/16
 */
public class AlinoDefaultWebSecurityManager extends DefaultWebSecurityManager {
    @Override
    public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {
        AuthenticationInfo info;
        try {
            info = this.authenticate(token);
        } catch (AuthenticationException var7) {
            AuthenticationException loggedIn = var7;
            try {
                this.onFailedLogin(token, loggedIn, subject);
            } catch (Exception ex) {
                LoggerUtils.info(getClass(),ex,"onFailedLogin method threw an exception.  Logging and propagating original AuthenticationException.");
            }
            LoggerUtils.debug(getClass(),var7.getMessage());
            String message = var7.getClass().getSimpleName();
            if ("ExcessiveAttemptsException".equals(message)) {
                var7 = new ExcessiveAttemptsException("密码输入错误次数过于频繁,请过30分钟后再试");
            } else if ("IncorrectCredentialsException".equals(message)) {
                var7 = new IncorrectCredentialsException("账号或密码错误");
            } else if ("UnknownAccountException".equals(var7.getMessage())) {
                var7 = new UnknownAccountException("账号或密码错误");
            } else if ("IncorrectCaptchaException".equals(message)) {
                //var7 = new IncorrectCaptchaException("验证码错误");
            } else if ("LockedAccountException".equals(message)) {
                var7 = new LockedAccountException(var7.getMessage());
            } else if ("DisabledAccountException".equals(message)) {
                var7 = new DisabledAccountException(var7.getMessage());
            }
            throw var7;
        }
        Subject loggedIn1 = this.createSubject(token, info, subject);
        this.onSuccessfulLogin(token, info, loggedIn1);
        return loggedIn1;
    }
}
